3. Bcp
The bcp utility provides bulk
copying of data between instances of Microsoft SQL Server. This utility
is installed with SQL Server and requires no knowledge or understanding
of T-SQL syntax. If you aren't familiar with the bcp utility, don't
confuse or associate its functionality with that of the Import/Export
Wizard in SQL Server. Although the bcp documentation refers to what bcp
does as a "bulk copy," be aware that you can't bcp data from a source
into a destination with a single statement. You must first bcp the data
out of the source; then, you can bcp the data in to the destination.
NOTE
The bcp utility is very
flexible and powerful, and you can apply a lot of options to it. This
section doesn't go into the entire range of bcp options or dive deep
into the many uses of the utility. You can find that information in the
SQL Server Books Online or on the Microsoft MSDN web site at http://msdn.microsoft.com/en-us/library/ms162802.aspx.
This section describe show to
use the bcp utility to export data from a local database and import the
data into your SQL Azure database. It also discusses some things you
should watch out for when using the bcp utility for SQL Azure.
3.1. Invoking BCP
The bcp utility has no GUI; it's
a command prompt–driven utility. But don't let that intimidate you,
especially given what you're using it for. It's very flexible and can
seem a bit overwhelming, but it's quite simple. The basic syntax for the
bcp utility is as follows:
bcp table direction filename -servername -username -password
where:
table is the source or destination table based on the direction parameter.
direction is in or out, depending on whether you're copying data into the database or out of the database.
filename is the filename you're copying data to or from.
servername is the name of the server you're copying data to or from.
username is the username used to connect to either the local or SQL Azure database.
password is the password associated with the username.
Let's get started by exporting the data from your source database.
3.2. Exporting the Data
Begin by copying data out of your local SQL instance. Open a command prompt, and type the command shown in Figure 17.
Enter your own values for the server name, the target directory, and
the username and password for your local server. (The password is
blanked out in Figure 17.)
Notice that in this example you're using the out keyword for the direction parameter. That's because you're copying data out of SQL Server.
The -n parameter performs the bulk-copy operation using the native database data types of the data. The -q parameter executes the SET QUOTED_IDENTIFIERS ON statement in the connection between the bcp utility and your SQL Server instance.
After you type in the command,
press the Enter key to execute the bcp utility. In mere milliseconds,
105 rows are exported and copied to the user.dat file. Now, do the same for the Docs and UserDocs tables.